<?php
	/* Template Name: Contact */
?>
<?php 
if( get_option('reedwan_contact_mail') ):
$contactEmail = get_option('reedwan_contact_mail');
else:
$contactEmail = get_option('admin_email');
endif;

if( get_option('reedwan_contact_subject') ):
$contactSubject = get_option('reedwan_contact_subject');
else:
$contactSubject = 'ShowyCase';
endif;

if(isset($_POST['submitted'])):

	// NAME CHECHING
	if(trim($_POST['contactName']) === '') {
	$nameError = __('You forgot to enter your name.', 'unicorn');
	$hasError = true;
	} else {
	$name = trim($_POST['contactName']);
	}
	
	// EMAIL CHECHING
	if(trim($_POST['email']) === '')  {
	$emailError = __('You forgot to enter your email address.', 'unicorn');
	$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$", trim($_POST['email']))) {
	$emailError = __('You entered an invalid email address.', 'unicorn');
	$hasError = true;
	} else {
	$email = trim($_POST['email']);
	}
	
	// MESSAGE CHECHING
	if(trim($_POST['comments']) === '') {
	$commentError = __('You forgot to enter your comments.', 'unicorn');
	$hasError = true;
	} else {
  	if(function_exists('stripslashes')) {
  	$comments = stripslashes(trim($_POST['comments']));
  	} else {
  	$comments = trim($_POST['comments']);
  	}
	}
	
	// IF EVERYTHING IS OK
	if(!isset($hasError)):
		
		$emailTo = $contactEmail;
		$subject = '['.$contactSubject.'] from '.$name;
		$body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
		$headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
		
		mail($emailTo, $subject, $body, $headers);
		
		$emailSent = true;
		
	endif;
	
endif;
?>
<?php get_header(); ?>
<script type="text/javascript">
	jQuery(window).load(function() {
////////////////////////// Contact Form /////////////////////////////////
	 jQuery('form#contactf').submit(function() {
  jQuery('form#contactf .error').remove();
  var hasError = false;
  jQuery('.required').each(function() {
   if(jQuery.trim(jQuery(this).val()) == '') {
    var labelText = jQuery(this).prev('label').text();
    jQuery(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
    hasError = true;
   } else if(jQuery(this).hasClass('email')) {
    var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?jQuery/;
    if(!emailReg.test(jQuery.trim(jQuery(this).val()))) {
     var labelText = jQuery(this).prev('label').text();
     jQuery(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
     hasError = true;
    }
   }
  });
  if(!hasError) {
   var formInput = jQuery(this).serialize();
   jQuery.post(jQuery(this).attr('action'),formInput, function(data){
    jQuery('form#contactf').slideUp("fast", function() {       
     jQuery(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent. I check my email all the time, so I should be in touch soon.</p>');
    });
   });
  }
  
  return false;
  
 });
 });
 </script>
	<?php 
		$sidebar_position = get_option('reedwan_sidebar_position', 'true');
		$featured_image = get_post_meta(get_the_ID(), 'reedwan_featured_image', true);
	?>
	<?php if($sidebar_position=='left'):  ?>
		<!-- Left Sidebar -->
		<div class="grid_4 sidebar">
			<?php get_sidebar();?>
		</div> 
	<?php endif; ?>

	<div class="grid_8 single-page">
		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>	
				<?php if($featured_image=='Yes'):  ?>
					<?php if (has_post_thumbnail()):?> 
						<div class="featured-image-post">
							<?php the_post_thumbnail('small-slider'); ?> 
						</div>
					<?php else: ?> 
						<div class="featured-image-post">
							<img src="<?php get_template_directory_uri() ?>/images/thumbnails/small-slider.png" alt="no image" />
						</div>
					<?php endif; ?>
				<?php endif; ?>
		<div class="single-title">
			<h1><?php the_title(); ?></h1>	
		</div>
		<div class="clear"></div>
		<div class="single-content">
			<?php the_content(); ?>
			<?php // CONTACT FORM
			if(isset($emailSent) && $emailSent == true): ?>
								
				<h2 class="thanks"><?php _e('Thanks, your email was successfully sent', 'unicorn') ?></h2>
				
			<?php else: ?>
				
				<?php if(isset($hasError)): ?>
			    <h2 class="error"><?php _e('There was an error submitting the form', 'unicorn') ?></h2>
				<?php endif; ?>
				
				<form action="<?php the_permalink(); ?>" id="contactf" method="post">
					
					<p>
						<label for="contactName"><?php _e('Name *', 'unicorn') ?></label>
						<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required" />
						<?php if(isset($nameError) && $nameError != '') { ?><em class="error"><?php echo $nameError;?></em><?php } ?>
					</p>
					
					<p>
						<label for="email"><?php _e('Email *', 'unicorn') ?></label>
						<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required email"/>
						<?php if(isset($emailError) && $emailError != '') { ?><em class="error"><?php echo $emailError;?></em><?php } ?>
					</p>
					
					<p>
						<label for="comments"><?php _e('Message *', 'unicorn') ?></label>
						<textarea name="comments" id="commentsText" rows="8" cols="45" class="required"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
						<span class="clear"></span>
						<?php if(isset($commentError) && $commentError != '') { ?><em class="error"><?php echo $commentError;?></em><?php } ?>
					</p>
					
					<p>
						<input type="hidden" name="submitted" id="submitted" value="true" />
						<button type="submit"><?php _e('Send Message', 'unicorn') ?></button>
					</p>
				</form>
			<?php endif; ?>
		</div>
		<?php endwhile; endif; ?>
	</div>
	<?php if($sidebar_position=='right'):  ?>
		<!-- Right Sidebar -->
		<div class="grid_4 sidebar">
			<?php get_sidebar();?>
		</div>
	<?php endif; ?>
<?php get_footer(); ?>